;
var helper = require("../../javascript/helper");
var request = require("request");
var db = require("../../javascript/mysql");
var mysql = new db.mysqlClient("accident");

var spider = function () {
    var that = this;
    this.name = "驾乘人员意外伤害保险"
    this.genProductInfo = function () {
        let projects = [];
        plan = {
            planA: {
                "days": ["365d"],
                "people_count": ["1"],
                "seat_count": ["5", "7"],
                "dirver_accident": ["100000"],
                "rider_accident": ["100000"],
                "dirver_medical": ["10000"],
                "rider_medical": ["10000"],
                "dirver_hospital": ["18000"],
                "rider_hospital": ["18000"]
            },
            planB: {
                "days": ["365d"],
                "people_count": ["1"],
                "seat_count": ["5", "7"],
                "dirver_accident": ["200000"],
                "rider_accident": ["200000"],
                "dirver_medical": ["10000"],
                "rider_medical": ["10000"],
                "dirver_hospital": ["18000"],
                "rider_hospital": ["18000"]

            },
            planC: {
                "days": ["365d"],
                "people_count": ["1"],
                "seat_count": ["5", "7"],
                "dirver_accident": ["300000"],
                "rider_accident": ["300000"],
                "dirver_medical": ["20000"],
                "rider_medical": ["20000"],
                "dirver_hospital": ["18000"],
                "rider_hospital": ["18000"]
            }
        }
        for (k in plan) {
            data = helper.product([
                plan[k]["days"], plan[k]["people_count"], plan[k]["seat_count"], plan[k]["dirver_accident"], plan[k]["rider_accident"], plan[k]["dirver_medical"], plan[k]["rider_medical"], plan[k]["dirver_hospital"], plan[k]["rider_hospital"]
            ])
            for (i = 0; i < data.length; i++) {
                dt = helper.formatProductInfo(data[i], plan[k]);
                projects.push(dt);
            }
        }
        return projects;

    }
    this.httpRequest = function (dt) {

        return new Promise(function (resolve, reject) {
            request({
                url: "https://www.ebgic.com/nvhl/countPrice.json",
                method: "POST",
                json: false,
                rejectUnauthorized: false,
                headers: {
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Connection": "keep-alive",
                    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                    "Cookie": "MEIQIA_TRACK_ID=1aFJ6Ncn7kUoAV8VJ7u8Tftm9x0; JSESSIONID1=m9Z3vuyZ-_n0XDFMl935VS6Ha1U7N7nNdYS9xUa0LVT-tG-vpvj5!-1746492695; MEIQIA_VISIT_ID=1cmw76BQfnqhbhVbvxUOVHCBphi",
                    "Sec-Fetch-Dest": "empty",
                    "Sec-Fetch-Mode": "cors",
                    "Sec-Fetch-Site": "same-origin",
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
                    "X-Requested-With": "XMLHttpRequest"
                },
                body: helper.urlencode(that.filterParams(dt))

            }, function (err, res, body) {
                if (!err && res.statusCode == 200) {
                    if (body.indexOf("totalPrice") != -1) {
                        price = JSON.parse(body).totalPrice.toFixed(2).toString();
                        rs = {
                            name: that.name,
                            company: "北部湾财产保险",
                            price: price
                        }
                        resolve(rs);
                    }
                }
            })
        })
    }
    this.filterParams = function (dt) {
        if (dt["dirver_accident"] == "100000") {
            if (dt["seat_count"] == "5") {
                params = {
                    "insuranceid_24196_保险金额": "100000",
                    "insuranceid_24197_保险金额": "400000",
                    "insuranceid_24198_保险金额": "10000",
                    "insuranceid_24199_保险金额": "40000",
                    "insuranceid_24200_保险金额": "18000",
                    "insuranceid_24201_保险金额": "72000",
                    "suiteId": "5925",
                    "period": "365",
                    "amount": "",
                    "personCount": "1",
                    "backInsuranceCount": "",
                    "youngAccident": "JcPersonalAccident"
                }
                return params;

            } else {
                params = {
                    "insuranceid_24214_保险金额": "100000",
                    "insuranceid_24215_保险金额": "600000",
                    "insuranceid_24216_保险金额": "10000",
                    "insuranceid_24217_保险金额": "60000",
                    "insuranceid_24218_保险金额": "18000",
                    "insuranceid_24219_保险金额": "108000",
                    "suiteId": "5928",
                    "period": "365",
                    "amount": "",
                    "personCount": "1",
                    "backInsuranceCount": "",
                    "youngAccident": "JcPersonalAccident"
                }
                return params;

            }

        } else if (dt["dirver_accident"] == "200000") {
            if (dt["seat_count"] == "5") {
                params = {
                    "insuranceid_24202_保险金额": "200000",
                    "insuranceid_24203_保险金额": "800000",
                    "insuranceid_24204_保险金额": "10000",
                    "insuranceid_24205_保险金额": "40000",
                    "insuranceid_24206_保险金额": "18000",
                    "insuranceid_24207_保险金额": "72000",
                    "suiteId": "5926",
                    "period": "365",
                    "amount": "",
                    "personCount": "1",
                    "backInsuranceCount": "",
                    "youngAccident": "JcPersonalAccident"
                }
                return params;

            } else {
                params = {
                    "insuranceid_24220_保险金额": "200000",
                    "insuranceid_24221_保险金额": "1200000",
                    "insuranceid_24222_保险金额": "10000",
                    "insuranceid_24223_保险金额": "60000",
                    "insuranceid_24224_保险金额": "18000",
                    "insuranceid_24225_保险金额": "108000",
                    "suiteId": "5929",
                    "period": "365",
                    "amount": "",
                    "personCount": "1",
                    "backInsuranceCount": "",
                    "youngAccident": "JcPersonalAccident"
                }
                return params;

            }
        } else {
            if (dt["seat_count"] == "5") {
                params = {
                    "insuranceid_24208_保险金额": "300000",
                    "insuranceid_24209_保险金额": "1200000",
                    "insuranceid_24210_保险金额": "20000",
                    "insuranceid_24211_保险金额": "80000",
                    "insuranceid_24212_保险金额": "18000",
                    "insuranceid_24213_保险金额": "72000",
                    "suiteId": "5927",
                    "period": "365",
                    "amount": "",
                    "personCount": "1",
                    "backInsuranceCount": "",
                    "youngAccident": "JcPersonalAccident"
                }
                return params;

            } else {
                params = {
                    "insuranceid_24226_保险金额": "300000",
                    "insuranceid_24227_保险金额": "1800000",
                    "insuranceid_24228_保险金额": "20000",
                    "insuranceid_24229_保险金额": "120000",
                    "insuranceid_24230_保险金额": "18000",
                    "insuranceid_24231_保险金额": "108000",
                    "suiteId": "5930",
                    "period": "365",
                    "amount": "",
                    "personCount": "1",
                    "backInsuranceCount": "",
                    "youngAccident": "JcPersonalAccident"
                }
                return params;

            }

        }

    }
    this.main = async function () {
        data = that.genProductInfo();
        for (i = 0; i < data.length; i++) {
            dt = data[i];
            // console.log( dt )
            let rs = await that.httpRequest(dt)
            let res = Object.assign( dt,rs );
            mysql.insertData( res );
        }
        mysql.close();
    }

}
